package net.jarang.android.support.http;

import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class BaseBlocHttpInvocator {
	protected String host;
	protected int port;
	protected String libraryName;
	protected int connectionTimeout;
	protected int soTimeout;
	protected String encoding;

	public BaseBlocHttpInvocator(String host, int port, String libraryName) {
		this(host, port, libraryName, 3 * 1000000, 3 * 1000000, "UTF-8");
	}
	
	public BaseBlocHttpInvocator(String host, int port, String libraryName, int connectionTimeout, int soTimeout, String encoding) {
		this.host = host;
		this.port = port;
		this.libraryName = libraryName;
		this.connectionTimeout = connectionTimeout;
		this.soTimeout = soTimeout;
		this.encoding = encoding;
	}
	
	protected String getUri(String beanName, String methodName) {
		return libraryName + "/" + beanName + "/" + methodName;
	}	
	
	protected void responseStatus(int statusCode) throws Exception {
		if ((statusCode / 100) != 2) {
			throw new Exception("Unexcepted result! status code is " + statusCode);
		}
	}
	
	protected void releaseConnection(HttpMethod method) {
		if (method != null) {
			method.releaseConnection();
		}		
	}
	
	protected HttpClient getHttpClient(String host, int port) {
		HttpClient client = HttpClientTemplate.getClient(host, port, connectionTimeout, soTimeout);
		return client;
	}	

	protected GetMethod getMethod(String beanName, String methodName, Map<String, Object> params) {
		GetMethod method = HttpMethodTemplate.getMethod(getUri(beanName, methodName), params, encoding);
		return method;
	}	
	
	protected PostMethod postMethod(String beanName, String methodName, Map<String, Object> params) {
		PostMethod method = HttpMethodTemplate.postMethod(getUri(beanName, methodName), params, encoding);
		return method;
	}
	
	protected PostMethod postMethodByMultipart(String beanName, String methodName, Map<String, Object> params) {
		PostMethod method = HttpMethodTemplate.postMethodForMultipart(getUri(beanName, methodName), params, encoding);
		return method;
	}	
	
//	protected PostMethod postMethodByJson(String beanName, String methodName, Object...params) {
//		PostMethod method = HttpMethodTemplate.postMethodByJson(getUri(beanName, methodName), encoding, params);
//		return method;
//	}		

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public int getSoTimeout() {
		return soTimeout;
	}

	public void setSoTimeout(int soTimeout) {
		this.soTimeout = soTimeout;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getLibraryName() {
		return libraryName;
	}

	public void setLibraryName(String libraryName) {
		this.libraryName = libraryName;
	}

	public int getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(int connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}
}
